package com.ErdosWebService.rfBulkGoods;

import com.ClassDef.BulkGoodsDetailVo;
import com.ClassDef.BulkGoodsVo;
import com.alibaba.fastjson.JSONObject;
import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.workflow.webservices.*;

import javax.jws.WebService;
import java.util.List;

/**
 * Production arrangement notice - bulk goods生产安排通知单-大货
 * Created by guo_zy on 2022/2/15.
 */
@WebService(endpointInterface= "com.ErdosWebService.rfBulkGoods.MsgServiceBulkGoods")
public class PANBulkGoodsCreateWorkflow implements MsgServiceBulkGoods{
    @Override
    public String getValue(BulkGoodsVo datavo) throws DocumentException {
        Logger log = Logger4jUtil.getLoggerByName("/action/api/PANApi/");
        BaseBean bb = new BaseBean();
        JSONObject jsonResult = new JSONObject();
        log.info("生产安排通知单-大货自动创建接口-开始：");
        try {
            log.info("请求对象：" + datavo.toString());
            //调单号/合同号
            String ddh = datavo.getDdh();
            //总数量
            String zsl = datavo.getZsl();
            //客户名称
            String khmc = datavo.getKhmc();
            //交期
            String jq = datavo.getJq();
            //营销部备注
            String bz = datavo.getBz();
            //申请人
            String sqr = datavo.getSqr();
            //申请人部门
            String sqrbm = datavo.getSqrbm();
            //明细表
            List<BulkGoodsDetailVo> detailList = datavo.getDetailList();
            //region [封装主表]
            WorkflowRequestTableField[] wrMain = new WorkflowRequestTableField[7];
            //调单号/合同号
            wrMain[0] = new WorkflowRequestTableField();
            wrMain[0].setFieldName("ddh");
            wrMain[0].setFieldValue(ddh);
            wrMain[0].setView(true);
            wrMain[0].setEdit(true);
            wrMain[0].setFieldOrder(0);
            wrMain[0].setMand(false);
            //总数量
            wrMain[1] = new WorkflowRequestTableField();
            wrMain[1].setFieldName("zsl");
            wrMain[1].setFieldValue(zsl);
            wrMain[1].setView(true);
            wrMain[1].setEdit(true);
            wrMain[1].setFieldOrder(0);
            wrMain[1].setMand(false);
            //客户名称
            wrMain[2] = new WorkflowRequestTableField();
            wrMain[2].setFieldName("khmc");
            wrMain[2].setFieldValue(khmc);
            wrMain[2].setView(true);
            wrMain[2].setEdit(true);
            wrMain[2].setFieldOrder(0);
            wrMain[2].setMand(false);
            //交期
            wrMain[3] = new WorkflowRequestTableField();
            wrMain[3].setFieldName("jq");
            wrMain[3].setFieldValue(jq);
            wrMain[3].setView(true);
            wrMain[3].setEdit(true);
            wrMain[3].setFieldOrder(0);
            wrMain[3].setMand(false);
            //营销部备注
            wrMain[4] = new WorkflowRequestTableField();
            wrMain[4].setFieldName("yxbbz");
            wrMain[4].setFieldValue(bz);
            wrMain[4].setView(true);
            wrMain[4].setEdit(true);
            wrMain[4].setFieldOrder(0);
            wrMain[4].setMand(false);
            //申请人
            String strSql = "select id,departmentid from hrmresource where loginid = '"+sqr+"' or workcode = '"+sqr+"'";
            RecordSet rsC = new RecordSet();
            rsC.execute(strSql);
            while (rsC.next()){
                sqr = rsC.getString("id");
                sqrbm = rsC.getString("departmentid");//申请人部门
            }
            wrMain[5] = new WorkflowRequestTableField();
            wrMain[5].setFieldName("sqr");
            wrMain[5].setFieldValue(sqr);
            wrMain[5].setView(true);
            wrMain[5].setEdit(true);
            wrMain[5].setFieldOrder(0);
            wrMain[5].setMand(false);
            //申请人部门
            wrMain[6] = new WorkflowRequestTableField();
            wrMain[6].setFieldName("sqrbm");
            wrMain[6].setFieldValue(sqrbm);
            wrMain[6].setView(true);
            wrMain[6].setEdit(true);
            wrMain[6].setFieldOrder(0);
            wrMain[6].setMand(false);
            String workflowid = bb.getPropValue("htProp", "workflowid3");
            log.info("workflowid的值：" + workflowid);
            //配置流程基本信息:workflowId
            WorkflowBaseInfo wbi = new WorkflowBaseInfo();
            wbi.setWorkflowId("136022");//workflowid);
            //填装主表相关字段对象
            WorkflowRequestTableRecord[] wrtri_m = new WorkflowRequestTableRecord[1];
            wrtri_m[0] = new WorkflowRequestTableRecord();
            wrtri_m[0].setWorkflowRequestTableFields(wrMain);
            //创建流程主表信息对象
            WorkflowMainTableInfo wmi_m = new WorkflowMainTableInfo();
            wmi_m.setRequestRecords(wrtri_m);

            WorkflowRequestInfo wrInfo = new WorkflowRequestInfo();
            wrInfo.setRequestLevel("0");//紧急程度
            wrInfo.setRequestName("【绒纺】生产安排通知单-大货");//流程的名称
            //0:停留在提交节点;1:自动流转到下一节点
            wrInfo.setIsnextflow("1");
            //主表数据
            wrInfo.setWorkflowMainTableInfo(wmi_m);
            wrInfo.setWorkflowBaseInfo(wbi);
            //endregion
            //明细表
            WorkflowDetailTableInfo[] wdti = new WorkflowDetailTableInfo[1];
            if (detailList != null) {
                wrtri_m = getDetail_1(log,detailList);
                wdti[0] = new WorkflowDetailTableInfo();
                wdti[0].setWorkflowRequestTableRecords(wrtri_m);
            }
            wrInfo.setWorkflowDetailTableInfos(wdti);
            wrInfo.setCreatorId("1");//流程创建人
            //endregion
            WorkflowServiceImpl wfsi = new WorkflowServiceImpl();
            String requestid = wfsi.doCreateWorkflowRequest(wrInfo, Integer.parseInt("1"));
            log.info("【绒纺】生产安排通知单-大货==" + requestid);
            if (!"".equals(requestid) && Integer.parseInt(requestid) > 0) {
                log.info("【绒纺】生产安排通知单-大货流程创建成功！");
                jsonResult.put("status", "S");
                jsonResult.put("message", "流程创建成功！");
            } else {
                jsonResult.put("status", "F");
                jsonResult.put("message", "【绒纺】生产安排通知单-大货流程创建失败,失败原因：找不到下一节点审批人！");
                log.info("【绒纺】生产安排通知单-大货流程创建失败,失败原因：找不到下一节点审批人！");
            }
        }catch (Exception ex) {
            log.info("解析SAP接口数据发生异常,异常信息：" + ex.toString());
            jsonResult.put("status", "F");
            jsonResult.put("message", "解析数据失败,异常信息:" + ex.toString());
        }finally {
            return jsonResult.toString();
        }
    }

    private WorkflowRequestTableRecord[] getDetail_1(Logger log, List<BulkGoodsDetailVo> detailOneList) {
        log.info("明细表1：" + detailOneList.toString());
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[detailOneList.size()];
        WorkflowRequestTableField[] wrti;
        int i = 0;
        for (BulkGoodsDetailVo d1 : detailOneList) {
            wrti = new WorkflowRequestTableField[13];
            //生产企业
            wrti[0] = new WorkflowRequestTableField();
            wrti[0].setFieldName("scqy");
            wrti[0].setFieldValue(d1.getScqy());
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            //客户PO号
            wrti[1] = new WorkflowRequestTableField();
            wrti[1].setFieldName("khpoh");
            wrti[1].setFieldValue(d1.getKhpoh());
            wrti[1].setView(true);
            wrti[1].setEdit(true);
            //工厂货号
            wrti[2] = new WorkflowRequestTableField();
            wrti[2].setFieldName("gchh");
            wrti[2].setFieldValue(d1.getGchh());
            wrti[2].setView(true);
            wrti[2].setEdit(true);
            //客户货号
            wrti[3] = new WorkflowRequestTableField();
            wrti[3].setFieldName("khhh");
            wrti[3].setFieldValue(d1.getKhhh());
            wrti[3].setView(true);
            wrti[3].setEdit(true);
            //样品货号
            wrti[4] = new WorkflowRequestTableField();
            wrti[4].setFieldName("yphh");
            wrti[4].setFieldValue(d1.getYphh());
            wrti[4].setView(true);
            wrti[4].setEdit(true);
            //品名
            wrti[5] = new WorkflowRequestTableField();
            wrti[5].setFieldName("pm");
            wrti[5].setFieldValue(d1.getPm());
            wrti[5].setView(true);
            wrti[5].setEdit(true);
            //针型
            wrti[6] = new WorkflowRequestTableField();
            wrti[6].setFieldName("zx");
            wrti[6].setFieldValue(d1.getZx());
            wrti[6].setView(true);
            wrti[6].setEdit(true);
            //纱支
            wrti[7] = new WorkflowRequestTableField();
            wrti[7].setFieldName("sz");
            wrti[7].setFieldValue(d1.getSz());
            wrti[7].setView(true);
            wrti[7].setEdit(true);
            //成分
            wrti[8] = new WorkflowRequestTableField();
            wrti[8].setFieldName("cf");
            wrti[8].setFieldValue(d1.getCf());
            wrti[8].setView(true);
            wrti[8].setEdit(true);
            //等级
            wrti[9] = new WorkflowRequestTableField();
            wrti[9].setFieldName("dj");
            wrti[9].setFieldValue(d1.getDj());
            wrti[9].setView(true);
            wrti[9].setEdit(true);
            //加权平均重
            wrti[10] = new WorkflowRequestTableField();
            wrti[10].setFieldName("jqphz");
            wrti[10].setFieldValue(d1.getJqphz());
            wrti[10].setView(true);
            wrti[10].setEdit(true);
            //折合件
            wrti[11] = new WorkflowRequestTableField();
            wrti[11].setFieldName("zhj");
            wrti[11].setFieldValue(d1.getZhj());
            wrti[11].setView(true);
            wrti[11].setEdit(true);
            //数量/件
            wrti[12] = new WorkflowRequestTableField();
            wrti[12].setFieldName("sl");
            wrti[12].setFieldValue(d1.getSl());
            wrti[12].setView(true);
            wrti[12].setEdit(true);

            wrtri[i] = new WorkflowRequestTableRecord();
            wrtri[i].setWorkflowRequestTableFields(wrti);
            i++;
        }
        return wrtri;
    }
}
